﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using RFMobile.Session;
using RFMobile.Machines;
using RFMobile.Operations;

namespace RFMobile
{
    public partial class newOperationScreen : Form
    {
        enum operationType {
            MANTENCION = 1,
            EMERGENCIA = 2,
            INSTALACION = 3
        };

        mobileSession mbSession = mobileSession.Instance;
        MachineData md;
        OperationData od;

        public newOperationScreen(Int32 comtec_id_machine)
        {
            InitializeComponent();
            
            od = new OperationData();
            od.Initialize();
            od.comtec_id_machine = comtec_id_machine;
            
            md = new MachineData(comtec_id_machine);
            
            codTextBox.Text = md.cod_mainteiner;
            statusBar1.Text = "Seleccione el Tipo de Operacion";
            
            this.ShowDialog();
        }

        private void setStatusItems(bool status)
        {
            codTextBox.Enabled = status;
            operationComboBox.Enabled = status;
            diagnosisTextBox.Enabled = status;
            repuestosTextBox.Enabled = status;
            notasTextBox.Enabled = status;
            dateTimePicker1.Enabled = status;
        }

        private bool enableItems()
        {
            if (operationComboBox.SelectedItem.ToString() != "" && diagnosisTextBox.Text != "" && repuestosTextBox.Text != "" && notasTextBox.Text != "")
            {
                return true;
            }
            
            return false;
        }

        private void operationComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch(operationComboBox.SelectedIndex)
            {
                case 0:
                    od.operation_type = (Int32)operationType.MANTENCION;
                    break;
                case 1:
                    od.operation_type = (Int32)operationType.EMERGENCIA;
                    break;
                case 2:
                    od.operation_type = (Int32)operationType.INSTALACION;
                    break;
            }

            statusBar1.Text = "Llene la hoja de diagnostico...";

            guardarMenuItem.Enabled = enableItems();
        }

        private void volverMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void salirMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void diagnosisTextBox_TextChanged(object sender, EventArgs e)
        {
            if (diagnosisTextBox.Text != "")
            {
                statusBar1.Text = "Siga con la siguiente hoja 2...";
            }
            guardarMenuItem.Enabled = enableItems();
        }

        private void repuestosTextBox_TextChanged(object sender, EventArgs e)
        {
            if (repuestosTextBox.Text != "")
            {
                statusBar1.Text = "Una vez termine de llenar, siga con la hoja Notas...";
            }
            guardarMenuItem.Enabled = enableItems();
        }

        private void notasTextBox_TextChanged(object sender, EventArgs e)
        {
            if (notasTextBox.Text != "")
            {
                statusBar1.Text = "Una vez termine de llenar, seleccione la fecha de la sgte. mantencion...";
            }
            guardarMenuItem.Enabled = enableItems();
        }

        private void guardarMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            if (enableItems())
            {
                od.changed_parts = repuestosTextBox.Text;
                od.comtec_id_user = mbSession.comtec_id_user;
                od.diagnosis = diagnosisTextBox.Text;
                od.notes = notasTextBox.Text;
                od.next_maintainment_date = dateTimePicker1.Value;
                // Guardar operacion
                if (od.saveOperation())
                {
                    MessageBox.Show("Operacion guardada exitosamente.", "Felicitaciones");
                    setStatusItems(false);
                    modifMenuItem.Enabled = true;
                    eliminarMenuItem.Enabled = true;
                    guardarMenuItem.Enabled = false;
                }
                else
                {
                    MessageBox.Show("No se ha podido guardar la operacion.", "Lo lamentamos");
                }
            }
            Cursor.Current = Cursors.Default;
        }

        private void tabControl1_GotFocus(object sender, EventArgs e)
        {
            switch (tabControl1.SelectedIndex)
            {
                case 0:
                    statusBar1.Text = "Seleccione el Tipo de Operacion";
                    break;
                case 1:
                    statusBar1.Text = "Llene la hoja de repuestos.";
                    break;
            }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            historyScreen hscreen = new historyScreen(od.comtec_id_machine);
        }
    }
}